/**
 * 
 */
package org.cmcc.pcap.packet.tcp;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.pcap4j.packet.IpPacket;
import org.pcap4j.packet.TcpPacket;

import java.sql.Timestamp;

/**
 * @Date 2018年4月19日 下午3:29:10
 * @author Amish
 */
public class TcpProducer  implements Runnable{
	private static Logger logger = LogManager.getLogger(TcpProducer.class);

     private IpPacket ipPacket;
     private TcpConnectionCache tcpConnectionCache;
     private Timestamp timestamp;

     
     public TcpProducer(IpPacket ipPacket, TcpConnectionCache tcpConnectionCache,Timestamp timestamp) {
    	 this.ipPacket = ipPacket;
    	 this.tcpConnectionCache = tcpConnectionCache;
    	 this.timestamp = timestamp;
     }
     
	@Override
	public void run() {
		try{
			TcpPacketDecorator tcpPacketDecorator; 
			boolean isFirst = false;
			TcpPacket tcpPacket = ipPacket.get(TcpPacket.class);
 			if( tcpPacket.getHeader().getSyn() && !tcpPacket.getHeader().getAck()){//第一条TCP消息，TCP三次握手第一条消息, syn为1 ack为0
				logger.debug("receive the first tcp message : "+ tcpPacket.getHeader().getSequenceNumberAsLong());
				isFirst = true;
			}else {
				isFirst = false;
			}
			tcpPacketDecorator = new TcpPacketDecorator(isFirst);
			tcpPacketDecorator.ipPacket = ipPacket;
			tcpPacketDecorator.tcpPacket = ipPacket.get(TcpPacket.class);
			tcpPacketDecorator.timestamp = this.timestamp;
			this.tcpConnectionCache.addTcpPacketDecorator(tcpPacketDecorator);
		}catch(Exception e){
			logger.error("TcpHandler has some exception : ",e);
		}
	}

}
